package com.wyg.epuser.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.wyg.epcommonbase.common.constant.SessionCST;
import com.wyg.epuser.entity.SysRole;
import com.wyg.epuser.entity.SysUserRole;
import com.wyg.epuser.entity.User;
import com.wyg.epuser.mapper.SysPermissionMapper;
import com.wyg.epuser.mapper.SysRoleMapper;
import com.wyg.epuser.mapper.SysRolePermissionMapper;
import com.wyg.epuser.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author: WangYongGan
 * @Date: 2021/12/20 18:56
 * @Description:
 */
@Component
public class RequestUtil implements InitializingBean {
    @Autowired
    SysUserRoleMapper SysUserRoleMapper_;
    @Autowired
    SysRoleMapper SysRoleMapper_;
    @Autowired
    SysPermissionMapper SysPermissionMapper_;
    @Autowired
    SysRolePermissionMapper SysRolePermissionMapper_;


    static SysUserRoleMapper userRoleMapper;
    static SysRoleMapper roleMapper;
    static SysPermissionMapper permissionMapper;
    static SysRolePermissionMapper rolePermissionMapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        userRoleMapper = SysUserRoleMapper_;
        roleMapper = SysRoleMapper_;
        permissionMapper = SysPermissionMapper_;
        rolePermissionMapper = SysRolePermissionMapper_;
    }

    //获得当前线程request
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    public static Set<String> getRolesString() {
        final Set<SysRole> roles1 = getRoles();
        Set<String> roles = new HashSet<>();
        for (SysRole sysRole : roles1) {
            roles.add(sysRole.getRoleName());
        }
        return roles;
    }

    public static Set<SysRole> getRoles() {
        final User user = getUser();
        QueryWrapper<SysUserRole> userRolesWrapper = new QueryWrapper<>();
        QueryWrapper<SysRole> rolesWrapper = new QueryWrapper<>();
        final List<SysUserRole> user_id = userRoleMapper.selectList(userRolesWrapper.eq("user_id", user.getId()));
        List<Integer> list = new ArrayList<>();
        for (SysUserRole userRole : user_id) {
            list.add(userRole.getRoleId());
        }
        final List<SysRole> sysRoleList = roleMapper.selectList(rolesWrapper.in("id", list));
        return new HashSet<>(sysRoleList);
    }

    public static Set<String> getPerms() {
        final Set<SysRole> roles1 = getRoles();
        Set<String> perms = null;
//        QueryWrapper<SysRolePermission>sysRolePermissionQueryWrapper=new QueryWrapper<>();
//        QueryWrapper<SysPermission>permissionWrapper=new QueryWrapper<>();
//        for (SysRole sysRole : roles1) {
//            final List<SysRolePermission> rolePermissions = rolePermissionMapper.selectList(sysRolePermissionQueryWrapper.eq("role_id", .getId()));
//            List<Integer>list=new ArrayList<>();
//            for (SysUserRole userRole : user_id) {
//                list.add(userRole.getRoleId());
//            }
//            final List<SysRole> sysRoleList = roleMapper.selectList(rolesWrapper.in("id", list));
//            Set<String>roles=new HashSet<>();
//            for (SysRole sysRole : sysRoleList) {
//                roles.add(sysRole.getRoleName());
//            }
//
//        }
        return perms;
    }


    public static void setUserWithSession(HttpSession session, User user) {
        session.setAttribute(SessionCST.USER, user);
    }

    //获得当前线程response
    public static HttpServletResponse getHttpServletResponse() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
    }

    public static User getUser() {
        return (User) getHttpServletRequest().getSession().getAttribute(SessionCST.USER);
    }

    public static boolean logined() {
        return getUser() != null;
    }

    public static void setUser(User user) {
        getHttpServletRequest().getSession().setAttribute(SessionCST.USER, user);
    }


    public static HttpSession getSession() {
        return getHttpServletRequest().getSession();
    }

}
